package com.idp.web.controller;

import com.idp.common.base.BaseController;
import com.idp.common.base.BaseResult;
import com.idp.common.constants.CommonConstants;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;
import com.idp.web.dto.DepartmentListDTO;
import com.idp.web.dto.DepartmentTreeDTO;
import com.idp.web.entity.Department;
import com.idp.web.service.IDepartmentService;
import com.idp.web.vo.DepartmentSearchVo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医院科室controller
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/2/28
 */
@Controller
@RequestMapping("/department")
public class DepartmentController extends BaseController {

    private Logger logger = Logger.getLogger(DepartmentController.class);

    @Resource
    private IDepartmentService departmentService;

    /**
     * 初始化模板管理页面
     *
     * @return String
     */
    @RequestMapping("/init")
    public String init() {
        return "backstage/department/departmentSearch";
    }

    /**
     * 根据条件分页查询科室列表
     *
     * @param request    HttpServletRequest
     * @return String
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        try {
            //列表信息
            DepartmentSearchVo departmentSearchVo = new DepartmentSearchVo();
            departmentSearchVo.setParentId("0");
            request.setAttribute("departmentList", departmentService.findForTreeTable(departmentSearchVo));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "backstage/department/departmentList";
    }

    /**
     * 查询下级科室
     * @param parentId 父id
     * @return String
     */
    @RequestMapping("/treeTable")
    @ResponseBody
    public String treeTable(String parentId){
        JSONArray json = new JSONArray();
        try {
            DepartmentSearchVo departmentSearchVo = new DepartmentSearchVo();
            departmentSearchVo.setParentId(parentId);
            json.addAll(departmentService.findForTreeTable(departmentSearchVo));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return json.toString();
    }

    /**
     * 获取科室树状结构
     *
     * @return String
     */
    @RequestMapping("/getDepartmentForTree")
    @ResponseBody
    public String getDepartmentForTree(Long currentId){
        JSONArray json = new JSONArray();

        List<DepartmentTreeDTO> departmentTreeList = departmentService.find(new DepartmentSearchVo());

        if(departmentTreeList != null && departmentTreeList.size() > 0){
            for(DepartmentTreeDTO departmentTreeDTO : departmentTreeList){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id", departmentTreeDTO.getId());
                map.put("name", departmentTreeDTO.getDepartmentName());
                map.put("pId", departmentTreeDTO.getParentId());
                json.add(map);
            }
        }

        return json.toString();
    }

    /**
     * 新增修改页面初始化
     *
     * @param id      科室id
     * @param request HttpServletRequest
     * @return String
     */
    @RequestMapping("/department")
    public String department(String id, HttpServletRequest request) {
        try {
            if (ValidateUtils.isNotEmpty(id)) {
                Department department = departmentService.getById(id);
                request.setAttribute("department", department);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "backstage/department/department";
    }

    /**
     * 保存
     *
     * @param department 科室信息
     * @return String
     */
    @RequestMapping("/save")
    @ResponseBody
    public String save(Department department) {
        JSONObject json = new JSONObject();
        boolean saveCheck = false;
        try {
            // 修改
            if (ValidateUtils.isNotEmpty(department.getId())) {
                saveCheck = departmentService.update(department,getUserId());
            }
            // 新增
            else {
                saveCheck = departmentService.add(department,null,getUserId());
            }
            if (saveCheck){
                json.put("result", "save_success");
            }else {
                json.put("result", "save_fail");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            json.put("result", "save_fail");
        }
        return json.toString();
    }

    /**
     * 删除
     *
     * @param id 科室id
     * @return String
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String id) {
        JSONObject json = new JSONObject();
        try {
            departmentService.delete(id);
            json.put("result", "delete_success");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            json.put("result", "delete_fail");
        }
        return json.toString();
    }

    /**
     * 查询科室的统计信息
     *
     * @return BaseResult
     */
    @RequestMapping("/queryStatistics")
    @ResponseBody
    public BaseResult queryStatistics() {
        JSONObject json = new JSONObject();
        //一级科室
        json.put("departmentWeightOne", departmentService.queryStatistics(CommonConstants.DEPARTMENT_WEIGHT_ONE,null));
        //二级科室科室
        json.put("departmentWeightTwo", departmentService.queryStatistics(CommonConstants.DEPARTMENT_WEIGHT_TWO,null));
        return sendResult200(json);
    }
}
